<template id="select-red-paper">
    <div v-if="paperInfo&&paperInfo.length>0">
        <div class="detail-tutorial f32 fr text-left border-bottom bg-fff border-20" @click="openList()">
            <div class="bonus fl f28">领红包 <em class="f24 color-fff ml-20" v-show="getPaperMoney(goodsEO)">{{getPaperMoney(goodsEO)}}</em></div>
            <a  class="sharembgs"></a>
        </div>
        <div class="dbag-guarantee fixed-bottom border-top bg-fff pb-30 " v-if="listShow">
            <div class="dbgtee-titl f36 color-333"><a class="close mr-30" @click="openList()"></a>领红包</div>
            <div class="dbgtee-bonus f24 color-999 text-center">以下使用条件均为商品金额，非订单金额</div>
            <div class="dbgteen-dbcon">
                <div class="dbgtee-blist border mx-30 mb-20" v-for="item in paperInfo" @click="getPaper(item)" v-if="goodsEO.price>=item.MinExpentiure">
                    <div class="sgbst-lpoer fl">
                        <div class="appico "></div>
                        <div class="appicotxt f22 color-fff">{{getTerminalModes(item.UseableTerminalModes)}}</div>
                        <h2 class="font-arial f36 color-m1 text-center">¥<i class="fontbold mx-10">{{item.CouponValue}}</i></h2>
                        <p class="color-m1 text-center f22">满{{item.MinMoney}}元可用</p>
                    </div>
                    <div class="sgbst-center"></div>
                    <div class="sgbst-rlker fr p-20 pt-30 ">
                        <div class="alved-ico" v-if="item.IsReceivedCoupon"></div>
                        <div class="alvedno-ico" v-if="item.SumNum==item.ReceivedSumNum&&!item.IsReceivedCoupon"></div>
                        <div class="sbrk-titl f26 color-333"><i class="ico_z text-center color-fff f24 mr-15">{{getBizOfferTypes(item.UseableBizOfferTypes  )}}</i>{{getGame(item.UseableGameList,item.UseableBizOfferTypes)}}</div>
                        <div class="sbrk-tran f24 color-666">{{getServiceTypes(item.UseableTradingServiceTypes)}}</div>
                        <div class="sbrk-tnbtn f20 color-999"><a class="fr border f26 color-m1 text-center border-color"  v-if="!item.IsReceivedCoupon&&item.SumNum!=item.ReceivedSumNum" >点击领取</a>{{getTime(item.UseableFrom,item.UseableTo,item.EffectiveNum)}}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
	import {mapState, mapGetters, mapActions} from 'vuex'
	export default {
		template: '#select-red-paper',
		props: ['goodsEO'],
		data() {
			return {
				listShow:false,
				paperInfo:[],
			}
		},
		mounted: function () {
			var self = this;
			var trading_type = '47'
			if(!self.goodsEO.id){
				trading_type = '42' + ''
            }
			self.$http.post(self.$CONSTANTS.APISearchCenter + 'OnoffService/selectOnoff', {"mapRequest": {"type": "红包开关"}})
				.then((res) => {
					var data = res.body;
					if(data.success&&data.result.status){
						var params = {"game_id":self.goodsEO.gameId,"bizoffertype_id":self.goodsEO.goodsType+'',"terminal_mode":"2","trading_type":trading_type,"coupon_grade":"2"}
						var promise = self.getRedPaper(params)
						promise.then(function (data) {
							if(data&&data.length>0){
								var list = []
								data.forEach(function (item) {
                                    if(self.goodsEO.price>=item.MinExpentiure){
	                                    list.push(item)
                                    }
								})
                            }
							self.paperInfo = list
						})
					}
				})
				.catch(function (res) {
					console.log(res)
				});
		},
		methods: {
			openList:function(){
				var self = this
				var self = this;
				var trading_type = '47'
				if(!self.goodsEO.id){
					trading_type = '42' + ''
				}

				var params = {"game_id":self.goodsEO.gameId,"bizoffertype_id":self.goodsEO.goodsType+'',"terminal_mode":"2","trading_type":trading_type,"coupon_grade":"2"}
				var promise = this.getLoginRedPaper(params)
				promise.then(function (data) {
					if(data){
						self.paperInfo = data
						self.listShow = !self.listShow
                    }else{
						location.href = "/vue/login?returnUrl=" + encodeURIComponent(location.href);
                    }
				})

            },
			getPaper:function (item) {
				var self = this;
				if(!item.IsReceivedCoupon&&item.SumNum!=item.ReceivedSumNum){
					item.IsReceivedCoupon = true
					var promise = this.receiveCouponToUser({coupon_rule_id:item.GroupId})
					promise.then(function (data) {
						var message = '领取成功'
						var icon ='revesuccer'
						if(!data.Result){
							message = data.ErrorMsg
							icon = 'revenumber'
						}
						self.$toast({
							message:message,
							iconClass:icon,
							duration: 2000
						});
					})
                }

			},
            getPaperMoney:function (goodsEO) {
				var str = ""
                if(goodsEO){
	                this.paperInfo.forEach(function (paper) {
	                	if(goodsEO.price>=paper.MinMoney){
			                str = '立减'+paper.CouponValue+'元'
                        }
	                })
                }
                return str
            },
            getTerminalModes(UseableTerminalModes){
				var str = '全站通用'
                var modesMap = {
	                0:'电脑版',1:'触屏版',2:'APP'
                }
                if(UseableTerminalModes){
	                var list = UseableTerminalModes.split(',')
                    if(list.length<3){
	                	var temp =[]
	                    list.forEach(function (item) {
		                    temp.push(modesMap[item])
	                    })
	                    str =  temp.join("\\") + '专享'
                    }
                }
                return str
            },
            getGame(UseableGameList,UseableBizOfferTypes ){
	            var str = '限购部分游戏帐号类商品'
                var getBizOfferTypes = this.getBizOfferTypes(UseableBizOfferTypes,true)
                if(getBizOfferTypes!='全部商品类型'){
	                getBizOfferTypes = getBizOfferTypes +'类商品'
                }
	            if(UseableGameList){
		            if(UseableGameList.split(',').length==1){
			            str = '限购'+this.goodsEO.gameName+getBizOfferTypes
		            }
	            }else {
		            str = '支持全部游戏'+getBizOfferTypes
	            }
	            return str
            },
            getBizOfferTypes  (UseableBizOfferTypes,all){
				var str = '通用'
                if(all){
	                str = '全部商品类型'
                }
                var modesMap = {
	                0:'装备',1:'游戏币',2:'帐号',3:'代练',
	                4:'包裹',5:'点卡',6:'网店',7:'密保卡',
	                8:'激活码',9:'手机充值卡',10:'其他',11:'元宝类',
                    12:'互换'
                }
                if(UseableBizOfferTypes){
	                var list = UseableBizOfferTypes.split(',')
                    if(list.length<13){
	                    var temp =[]
	                    list.forEach(function (item) {
		                    temp.push(modesMap[item])
	                    })
	                    str =  temp.join(',')
                    }
                }
                return str
            },
            getServiceTypes  (UseableTradingServiceTypes){
				var str = '支持所有交易方式'
                var modesMap = {
	                0:'端游自助',1:'端游担保',2:'端游寄售',3:'API`',
	                4:'点卡类',5:'端游帐号',6:'商城',7:'消费券',
	                8:'工具',9:'代练',10:'未定义',11:'51淘',
	                12:'施乐会',13:'7宝',14:'658',15:'活动',
	                16:'赠金',17:'消息中心',18:'台服085',19:'173库存系统',
                    20:'平放开台',21:'积分频道',22:"彩票",23:'点卡商城',
                    24:'平放开台',25:'积分频道',26:"5173助理",27:'9517 B2B',
                    28:'帐号租赁',29:'手游交易',30:"直通车",31:'打赏',
                    32:'视频真人秀',33:'卖家保险',34:"网页游戏开服表",35:'点卡换金',
                    36:'点卡保险',37:'点卡竞价',38:"金牌掌柜",39:'658金融网',
                    40:'(游戏币)金币商城',41:'高尔夫',42:"手游",43:'游戏币回购',
                    44:'巨人合作金币',45:'退游宝',46:"金钻",47:'触屏版M站',
                    48:'饰品交易'
                }
                if(UseableTradingServiceTypes){
	                var list = UseableTradingServiceTypes.split(',')
                    if(list.length<13){
	                    var temp =[]
	                    list.forEach(function (item) {
		                    temp.push(modesMap[item])
	                    })
	                    str =  temp.join(',')+'商品交易'
                    }
                }
                return str
            },
            getTime(UseableFrom,UseableTo,EffectiveNum){
				var str = ''
                if(UseableFrom){
	                UseableFrom=UseableFrom.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1.$2.$3")
	                UseableTo=UseableTo.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1.$2.$3")
	                str = UseableFrom + "-"+ UseableTo
                }else{
//                	var nowDate = new Date();
//                	var endTime = new Date(EffectiveNum*24*60*60*1000+nowDate.getTime())
//	                str = nowDate.getFullYear()+'.'+(nowDate.getMonth()+1)+'.'+nowDate.getDate()+'-'+endTime.getFullYear()+'.'+(endTime.getMonth()+1)+'.'+endTime.getDate()
                    str = '领取后'+EffectiveNum+'天内有效'
                }
	            return str
            },
			...mapActions({
				getRedPaper: 'account/GET_REDPAPER',
				getLoginRedPaper: 'account/GET_LOGINREDPAPER',
				receiveCouponToUser: 'account/RECEIVE_REDPAPER',
			})
		},
	}
</script>